<template>
  <div class="user-info">
    <!-- 个人信息 -->
    <el-form ref="userForm" label-width="220px" :model="userInfo">
      <!--手机 -->
      <el-row class="inline-info">
        <el-col :span="12">
          <el-form-item label="手机">
            <el-input v-model="userInfo.mobile" />
          </el-form-item>
        </el-col>
      </el-row>
      <!-- 工号 入职时间 -->
      <el-row class="inline-info">
        <el-col :span="12">
          <el-form-item label="入职时间">
            <el-date-picker
              v-model="userInfo.timeOfEntry"
              type="date"
              class="inputW"
              value-format="yyyy-MM-dd"
            />
          </el-form-item>
        </el-col>
      </el-row>
      <!-- 员工照片 -->
      <el-row class="inline-info">
        <el-col :span="12">
          <el-form-item label="员工头像">
            <!-- 放置上传图片 -->
            <!-- <upload-img ref="staffPhoto" /> -->
            <upload-img v-model="staffPhoto" />
            <!-- <img src="" alt=""> -->
          </el-form-item>
        </el-col>
      </el-row>
      <!-- 保存个人信息 -->
      <el-row class="inline-info" type="flex" justify="center">
        <el-col :span="12">
          <el-button type="primary" @click="hSaveFn">保存更新</el-button>
          <el-button @click="$router.back()">返回</el-button>
        </el-col>
      </el-row>
    </el-form>
  </div>
</template>
<script>
import { getUserDetailById } from '@/api/user'
import { saveUserDetailByIdAPI } from '@/api/employees'
export default {
  data() {
    return {
      userInfo: {
        id: this.$route.query.id,
        mobile: '',
        timeOfEntry: ''
      },
      staffPhoto: ''
    }
  },
  created() {
    this.getUserDetail()
  },
  methods: {
    async getUserDetail() {
      const res = await getUserDetailById(this.userInfo.id)
      console.log('请求回来的数据login', res)
      this.userInfo = res
      // 把请求回来的数据,直接赋值给子组件里面imageUrl属性值
      // this.$refs.staffPhoto.imageUrl = res.staffPhoto
      this.staffPhoto = res.staffPhoto
      // console.log('父组件中图片地址', res.staffPhoto)
    },
    async doSaveFn() {
      try {
        // const newPhotoUrl = this.$refs.staffPhoto.imageUrl
        const newPhotoUrl = this.staffPhoto
        console.log('上传的文件地址是', newPhotoUrl)
        // 把新的图片地址,点击更新的时候,存到服务器
        this.userInfo.staffPhoto = newPhotoUrl
        const res = await saveUserDetailByIdAPI(this.userInfo)
        console.log('更新之后后端返回的数据', res)
        this.$message.success('更新成功')
        // 更新成功后返回上一个页面
        this.$router.back()
      } catch (error) {
        this.$message.error(error.message)
      }
    },
    hSaveFn() {
      this.$refs.userForm.validate(valid => {
        valid && this.doSaveFn()
      })
    }
  }
}
</script>

<style scoped>
  .user-info{
    padding-top:20px;
  }
</style>

